/**
 * Package for EnterpriseAssets area user permissions classes.
 */
jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.userMgt.permissions", function() {
});

/****************************************************************
 *  Class used to set permission and remove permission on the selected items in a matrix list.
 *  Author: Ramin Danaei Boroumand
 *  Date: 06-29-2007
 ****************************************************************/
jsx3.Class.defineClass(
        'tibco.admin.enterpriseAssets.userMgt.permissions.SetPermissions',
        jsx3.gui.Block,
        null,
        function(permission) {



            /**
            * Initializes an instance of this class
            */
            permission.prototype.initialize = function(actType, entityID, entityName, list) {
                this.actType = actType;
                this.entityID = entityID;
                this.entityName = entityName;
                this.mtxList = list;
                this.setPermissionBtn = this.getDescendantOfName("btnSetPermission27A");
                this.addPermissionBtn = this.getDescendantOfName("mBtnAddPermission27A");
            }

            /**
             *	Load the "Set Permission" window - fire by clicking the "Set Permission" button
             */
            permission.prototype.setPermission = function() {
                var selectedNodes = this.mtxList.getSelectedNodes();
                var topObj = APP.tibco.admin.getBodyBlock();
                if (selectedNodes.size() == 1)
                {
                    var curNode = selectedNodes.get(0);
                    var popupWin = tibco.admin.Main.loadUIResource(topObj, "tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/addUsersNGroups.xml", true);
					popupWin.setPermission(this.actType, curNode.getAttribute(this.entityName), curNode.getAttribute(this.entityID));
                }
                else {
                    if (selectedNodes.size() > 1) {
                        var addWin = tibco.admin.Main.loadUIResource(topObj, "tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/addUsersNGroups.xml", true);
                        var stype = this.actType;
                        var nameIdArray = new Array;
                        for (var i = 0; i < selectedNodes.size(); i++)
                        {
                            var curNode = selectedNodes.get(i);
                            var obj = new Object();
                            obj.id = curNode.getAttribute(this.entityID);
                            obj.name = curNode.getAttribute(this.entityName);
                            nameIdArray.push(obj);
                        }
                        addWin.setBulkPermission(stype, nameIdArray);
                    }
                }
            }

            /**
             *	Remove Permissions
             */
            permission.prototype.removePermission = function() {
                var msg = new Object();
                msg.entityType = this.actType;
                msg.entities = [];
                // create selected list
                var selectedNodes = this.mtxList.getSelectedNodes();
                var selectedNumber = selectedNodes.size();
                for (var i = 0; i < selectedNumber; i++)
                {
                    msg.entities.push({id: selectedNodes.get(i).getAttribute(this.entityID),
							name: selectedNodes.get(i).getAttribute(this.entityName)});
                }

                var removePermissions = tibco.admin.Main.loadUIResource(tibco.admin.Main.getServer().getBodyBlock(), "tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/removePermissionsM.xml");
				removePermissions.initialize(msg);
            }

            /**
             *  Set the display restriction of the "Set Permission" button and "Set Permission" menu button
             *  While there's no record was selected, then hide these two buttons
             *  while select one record, the "Set Permission" button will be shown
             *  While more than one records are selected , "Set Permissions" menu button will be shown.
             */
            permission.prototype.switchPermissionBtn = function() {
                var hasOwner = "true";
                var selectedNodes = this.mtxList.getSelectedNodes();
                if (selectedNodes.size() == 0) {
                    this.setPermissionBtn.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.addPermissionBtn.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    return;
                }
                for (var i = 0; i < selectedNodes.size(); i++) {
                    hasOwner = selectedNodes.get(i).getAttribute("hasOwner")
                    if (hasOwner == 'false')
                        break
                }
                if (hasOwner == 'false') {
                    this.setPermissionBtn.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.addPermissionBtn.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                }
                else {
                    if (selectedNodes.size() == 1) {
                        this.setPermissionBtn.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                        this.addPermissionBtn.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    }
                    else if (selectedNodes.size() > 1) {
                        this.setPermissionBtn.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.addPermissionBtn.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    }
                }
            }

            //=========== END OF SET PERMISSION ===============================================

        });
